package com.sgb.kjwl.view.ui.activity.business;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMemberListBinding;
import com.sgb.kjwl.di.business.BusinessModule;
import com.sgb.kjwl.di.business.DaggerBusinessComponent;
import com.sgb.kjwl.model.entity.business.MemberEntity;
import com.sgb.kjwl.view.ui.adapter.business.MemberListAdapter;
import com.sgb.kjwl.viewmodel.business.MemberListModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class MemberListActivity extends AppActivity {
    private ActivityMemberListBinding binding;
    private MemberListAdapter memberListAdapter;
    @Inject
    MemberListModel viewModel;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerBusinessComponent.builder().appComponent(appComponent).businessModule(new BusinessModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MemberListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(MemberListActivity.this, R.layout.activity_member_list);
        binding.setViewModel(viewModel);
    }

    @Override
    protected void onStart() {
        super.onStart();
        viewModel.getData();
    }

    @Override
    protected void init() {
        binding.include.ivBack.setOnClickListener(this);
        binding.tvAddMember.setOnClickListener(this);
        binding.include.titleView.setText("会员管理");

        if (memberListAdapter == null) {
            memberListAdapter = new MemberListAdapter(MemberListActivity.this, null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(MemberListActivity.this);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(memberListAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getData();
                binding.recyclerView.setLoadingMoreEnabled(true);
            }

            @Override
            public void onLoadMore() {
                if (memberListAdapter.getDatas().size() < viewModel.total) {
                    viewModel.getMoreData();
                } else {
                    binding.recyclerView.loadMoreComplete();
                }
            }
        });

        memberListAdapter.setOnItemClick(new AdapterOnItemClick<MemberEntity>() {
            @Override
            public void onItemClick(MemberEntity memberEntity, int position) {
                MemberDetailActivity.start(MemberListActivity.this, memberEntity.getId());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_addMember:
                AddMemberActivity.start(MemberListActivity.this);
                break;
        }
    }

    public void showData(List<MemberEntity> list) {
        binding.recyclerView.refreshComplete();
        if (list.size() == 0) {
            ArrayList<MemberEntity> memberEntities = new ArrayList<>();
            memberListAdapter.setDatas(memberEntities);
            binding.emptyView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.setVisibility(View.VISIBLE);
            binding.emptyView.setVisibility(View.GONE);
            memberListAdapter.setDatas(list);
        }
    }

    public void showMoreData(List<MemberEntity> list) {
        binding.recyclerView.loadMoreComplete();
        binding.emptyView.setVisibility(View.GONE);
        memberListAdapter.addDatas(list);
    }
}
